package com.lingyu.jsjx.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.lingyu.jsjx.domain.TeachingReflection;
import com.lingyu.jsjx.domain.dto.StudentScoreDTO;
import com.lingyu.jsjx.service.impl.StudentScoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.lingyu.jsjx.domain.StudentScore;
import com.lingyu.jsjx.service.IStudentScoreService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生成绩Controller
 *
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/studentScore")
public class StudentScoreController extends BaseController {
    @Autowired
    private IStudentScoreService studentScoreService;

    /**
     * 查询学生成绩列表
     */
    @RequiresPermissions("jsjx:studentScore:list")
    @GetMapping("/list")
    public TableDataInfo list(StudentScoreDTO studentScoreDTO) {
        startPage();
        List<StudentScore> list = studentScoreService.selectStudentScoreList(studentScoreDTO);
        return getDataTable(list);
    }

    /**
     * 导出学生成绩列表
     */
    @RequiresPermissions("jsjx:studentScore:export")
    @Log(title = "学生成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentScoreDTO studentScoreDTO) {
        List<StudentScore> list = studentScoreService.selectStudentScoreList(studentScoreDTO);
        ExcelUtil<StudentScore> util = new ExcelUtil<StudentScore>(StudentScore.class);
        util.exportExcel(response, list, "学生成绩数据");
    }

    /**
     * 获取学生成绩详细信息
     */
    @RequiresPermissions("jsjx:studentScore:query")
    @GetMapping(value = "/{scoreId}")
    public AjaxResult getInfo(@PathVariable("scoreId") Long scoreId) {
        return success(studentScoreService.selectStudentScoreByScoreId(scoreId));
    }

    /**
     * 新增学生成绩
     */
    @RequiresPermissions("jsjx:studentScore:add")
    @Log(title = "学生成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentScore studentScore) {
        int i = studentScoreService.insertStudentScore(studentScore);
        if(i > 0){
            return success(studentScore.getScoreId());
        }else{
            return toAjax(0);
        }
    }

    /**
     * 修改学生成绩
     */
    @RequiresPermissions("jsjx:studentScore:edit")
    @Log(title = "学生成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentScore studentScore) {
        return toAjax(studentScoreService.updateStudentScore(studentScore));
    }

    /**
     * 删除学生成绩
     */
    @RequiresPermissions("jsjx:studentScore:remove")
    @Log(title = "学生成绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scoreIds}")
    public AjaxResult remove(@PathVariable Long[] scoreIds) {
        return toAjax(studentScoreService.deleteStudentScoreByScoreIds(scoreIds));
    }

    @GetMapping(value = "/studentScoreStatistic")
    public AjaxResult queryStudentScoreStatistic() {
        return success(studentScoreService.selectStudentScoreCountByUserId());
    }

    @Log(title = "学情成绩", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<StudentScore> util = new ExcelUtil<>(StudentScore.class);
        List<StudentScore> studentScoreList = util.importExcel(file.getInputStream());
        String message = studentScoreService.importStudentScore(studentScoreList, false);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<StudentScore> util = new ExcelUtil<>(StudentScore.class);
        util.importTemplateExcel(response, "学情成绩数据");
    }
}
